import {observable, action} from "mobx";
import {Toast, Tools} from "weaver-mobile";
import {ListViewPage} from "weaver-mobile-page";
import * as API from "../../api/cptApi";
import {_scanQRCode} from "../../util";

const getLabel = Tools.getLabel;

export default class InventoryStore {
    @observable nowTab = 0;//当前tab:0左侧 1右侧
    @observable searchValue = "";
    @observable searchValue2 = "";
    @observable showCheck = false;
    @observable needInventoryList = new ListViewPage.Store({route: "/index/inventory/0"});
    @observable inventoriedList = new ListViewPage.Store({route: "/index/inventory/1"});
    @action
    init = () => {
        this.needInventoryList.clearData();
        this.inventoriedList.clearData();
        this.getMyInventoryList(this.needInventoryList, {"inventorystate": 0});
        this.getMyInventoryList(this.inventoriedList, {"inventorystate": 1});
    }
    @action
    onTabChange = (index) => {
        this.nowTab = index;
    };

    @action
    changeSearchValue = (val) => {
        if (this.nowTab === 0) {
            this.searchValue = val;
        } else {
            this.searchValue2 = val;
        }
    };
    @action
    onSearch = () => {
        let list, params = {};
        if (this.nowTab === 0) {
            list = this.needInventoryList;
            params = {"inventorystate": 0, name: this.searchValue};
        } else {
            list = this.inventoriedList;
            params = {"inventorystate": 1, name: this.searchValue2};
        }
        // 清除列表数据
        list.clearData();
        // 带参数请求列表数据
        this.getMyInventoryList(list, params)
    };
    getMyInventoryList = (list, params) => {
        API.getMyInventoryList(params).then(res => {
            list.getData({dataKey: res.datas});
        })
    };

    @action
    submitInfo = (id) => {
        const params = {
            inventoryId: id
        };
        API.doSubmitInventory(params).then(res => {
            if (res.flag) {
                Toast.success(getLabel(30700, "操作成功"), 2);
                this.init();
            } else {
                Toast.fail(res.msg, 2);
            }
        })
    };
    @action
    scanInfo = (e, history) => {
        window.event ? window.event.cancelBubble = true : e.stopPropagation();
        _scanQRCode((data) => {
            const params = {
                mark: data,
                scantype: "inventory"
            };
            API.scanQRCodeCapital(params).then(res => {
                if (res.flag) {
                    history.push("/inventoryForm/-1/" + res.capitalid + "/2");
                } else {
                    Toast.fail(res.msg, 2)
                }
            })
        })
    }
}
